package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.user.IMemberService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Address;
import com.mall.user.dal.persistence.AddressMapper;
import com.mall.user.dto.QueryMemberRequest;
import com.mall.user.dto.QueryMemberResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 *  ciggar
 * create-date: 2019/8/1-下午4:47
 *
 */
@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {

    @Autowired
    AddressMapper addressMapper;

    @Reference(check = false)
    private IMemberService memberService;

    /**
     * 验证用户合法性
     * @return
     */

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext=(CreateOrderContext)context;
        QueryMemberRequest request=new QueryMemberRequest();
        request.setUserId(createOrderContext.getUserId());
        QueryMemberResponse response = memberService.queryMemberById(request);
        if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            String username=response.getUsername();
            // 根据adressId获取userId
            Example example = new Example(Address.class);
            Address address = addressMapper.selectByPrimaryKey(createOrderContext.getAddressId());
            /*if(!username.equals(createOrderContext.getUserName())){*/
            if (address.getUserId() != createOrderContext.getUserId()) {
                throw new BizException(response.getCode(),response.getMsg());
            }
            createOrderContext.setBuyerNickName(username);
        }
        return true;
    }
}
